package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.pojo.Relationship;
import com.example.demo.service.Rservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/relationships")
public class RelationshipController {

    @Autowired
    private Rservice relationshipService;
    @GetMapping("/person/{personId}")
    public List<Relationship> listByPersonId(@PathVariable Long personId) {
        return relationshipService.listByPersonId(personId);
    }
    @GetMapping("/type/{relationType}")
    public List<Relationship> listByRelationType(@PathVariable String relationType) {
        return relationshipService.listByRelationType(relationType);
    }
    @GetMapping("/page")
    public Page<Relationship> pageRelations(
            @RequestParam(required = false) Long personId,
            @RequestParam(required = false) String relationType,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Relationship> page = new Page<>(pageNum, pageSize);
        return relationshipService.pageRelations(page, personId, relationType);
    }
    @PostMapping("/batch")
    public boolean batchCreateRelations(@RequestBody List<Relationship> relationships) {
        return relationshipService.batchCreateRelations(relationships);
    }
    @GetMapping("/exists")
    public boolean existsRelation(
            @RequestParam Long personId,
            @RequestParam Long relatedPersonId,
            @RequestParam String relationType) {
        return relationshipService.existsRelation(personId, relatedPersonId, relationType);
    }
}